#include "access_service.h"

#include "tcp_service.h"
#include <Windows.h>


BOOL APIENTRY DllMain(HANDLE hHandle, DWORD ul_reason_for_call, PVOID pReserved)
{
	switch (ul_reason_for_call) {
	case DLL_PROCESS_ATTACH:
		break;
	case DLL_PROCESS_DETACH:
		break;
	case DLL_THREAD_ATTACH:
		break;
	case DLL_THREAD_DETACH:
		break;
	}
	return TRUE;
}

static cmtd_access::TcpService * g_asInst = NULL;
static int gRefCount = 0;

ACCESS_API long __stdcall AS_Init()
{
	if (g_asInst == NULL) {
		g_asInst = new cmtd_access::TcpService();
		gRefCount = 1;
	}
	else {
		gRefCount++;
	}
	return 0;
}

ACCESS_API void __stdcall AS_CleanUp()
{
	gRefCount--;
	if (gRefCount < 0) {
		gRefCount = 0;
	}
	if (gRefCount == 0) {
		if (g_asInst) {
			delete g_asInst;
			g_asInst = NULL;
		}
	}
}

ACCESS_API long _stdcall AS_StartService(int nServicePort_, 
	cmtd::fMessageCallback msgCB_, void * userdata_)
{
	long result = -1;
	if (g_asInst) {
		result = g_asInst->run(nServicePort_, msgCB_, userdata_);
	}
	return result;
}

ACCESS_API long __stdcall AS_StopService()
{
	long result = -1;
	if (g_asInst) {
		result = g_asInst->stop();
	}
	return result;
}

ACCESS_API long __stdcall AS_Push(cmtd::PUSH_PARAMETER_PTR pPushParam_)
{
	long result = -1;
	if (g_asInst) {
		result = g_asInst->push(pPushParam_);
	}
	return result;
}

ACCESS_API long __stdcall AS_SetMessageCallBack(cmtd::fMessageCallback msgcb_,
	void * userdata_)
{
	long result = -1;
	if (g_asInst) {
		g_asInst->setMessageCallback(msgcb_, userdata_);
	}
	return result;
}